﻿using LanguagePolish.Base;
using LanguagePolish.EntityModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace LanguagePolish.Controls.WeekCalendar
{
    public class WeekCalendarViewModel : BaseViewModel
    {
        private static ILessonsProvider FakeLessonsProvider = new FakeLessonsProvider();

        private WeekCalendarModel model;
        
        private ILessonsProvider lessonsProvider;
        public ILessonsProvider LessonsProvider 
        {
            get 
            {
                if (lessonsProvider == null)
                    return FakeLessonsProvider;
                return lessonsProvider;
            }
            set
            {
                lessonsProvider = value;
                ReloadLessons();
            }
        }

        IEnumerable<Lesson> lessons;
        public IEnumerable<Lesson> CurrentLessons { get { return lessons; } }

        public DateTime CurrentDate { get { return model.CurrentDate; } }

        public ICommand PrevWeek { get; private set; }
        public ICommand NextWeek { get; private set; }

        public WeekCalendarViewModel()
        {
            model = new WeekCalendarModel();
            PrevWeek = new RelayCommand(DecrementWeek);
            NextWeek = new RelayCommand(IncrementWeek);
            ReloadLessons();
        }

        private void IncrementWeek()
        {
            model.IncrementWeek();
            OnPropertyChanged("CurrentDate");
            ReloadLessons();
        }

        private void DecrementWeek()
        {
            model.DecrementWeek();
            OnPropertyChanged("CurrentDate");
            ReloadLessons();
        }

        public void ReloadLessons()
        {
            DateTime from = model.FirstDay;
            DateTime to = model.LastDay;
            lessons = LessonsProvider.GetLessons(from, to);
            OnPropertyChanged("CurrentLessons");
        }
    }
}
